package org.example.myleet.p589;

import org.example.myleet.model.Node;

import java.util.ArrayList;
import java.util.List;

public class Solution {
    public List<Integer> preorder(Node root) {
        List<Integer> result = new ArrayList<>();
        if (null != root) {
            visitNode(result, root);
        }
        return result;
    }

    private void visitNode(List<Integer> result, Node node) {
        result.add(node.val);
        if (null == node.children || 0 == node.children.size()) {
            return;
        }
        for (Node child : node.children) {
            visitNode(result, child);
        }
    }
}
